//
//  NumberAnalysisViewController.m
//  CP20180523
//
//  Created by apple on 2018/5/27.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "NumberAnalysisViewController.h"

@interface NumberAnalysisViewController ()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightCons_1;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightCons_2;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightCons_3;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightCons_4;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightCons_5;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightCons_6;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightCons_7;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightCons_8;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightCons_9;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightCons_10;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightCons_11;





@property (weak, nonatomic) IBOutlet UIView *numberBoxView;


@end

@implementation NumberAnalysisViewController{
    NSMutableArray* selectedNumberArray;
    NSMutableArray* comparingArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"号码分析";
    // Do any additional setup after loading the view.
    selectedNumberArray = [NSMutableArray arrayWithCapacity:0];
    
    self.heightCons_1.constant = 0;
    self.heightCons_2.constant = 0;
    self.heightCons_3.constant = 0;
    self.heightCons_4.constant = 0;
    self.heightCons_5.constant = 0;
    self.heightCons_6.constant = 0;
    self.heightCons_7.constant = 0;
    self.heightCons_8.constant = 0;
    self.heightCons_9.constant = 0;
    self.heightCons_10.constant = 0;
    self.heightCons_11.constant = 0;
    
    
    comparingArray = [NSMutableArray arrayWithCapacity:0];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self randomCountAction];
    });
    
    
}

-(void)randomCountAction{
    CGFloat originalHeight = 120;
    NSArray* numberArray = @[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11"];
    
    NSMutableArray* saveArray = [NSMutableArray arrayWithCapacity:0];//保存今天的随机高度
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd";
    NSDate* nowDate = [NSDate date];
    NSString* keyStringForToday = [formatter stringFromDate:nowDate];
    
    
    
    NSArray* checkArray = [[NSUserDefaults standardUserDefaults] objectForKey:keyStringForToday];
    
    if ([checkArray count]>0) {
        /***
         已保存高度
         ***/
        for (int i=0; i<[numberArray count]; i++) {
            NSDictionary* oldDic = checkArray[i];
            CGFloat newHeight = [[oldDic objectForKey:numberArray[i]] floatValue];
            switch (i+1) {
                case 1:
                    self.heightCons_1.constant = newHeight;
                    break;
                case 2:
                    self.heightCons_2.constant = newHeight;
                    break;
                case 3:
                    self.heightCons_3.constant = newHeight;
                    break;
                case 4:
                    self.heightCons_4.constant = newHeight;
                    break;
                case 5:
                    self.heightCons_5.constant = newHeight;
                    break;
                case 6:
                    self.heightCons_6.constant = newHeight;
                    break;
                case 7:
                    self.heightCons_7.constant = newHeight;
                    break;
                case 8:
                    self.heightCons_8.constant = newHeight;
                    break;
                case 9:
                    self.heightCons_9.constant = newHeight;
                    break;
                case 10:
                    self.heightCons_10.constant = newHeight;
                    break;
                case 11:
                    self.heightCons_11.constant = newHeight;
                    break;
                default:
                    break;
            }
            //比较大小
            [self comparingActionWith:@{numberArray[i]:@(newHeight)}];
        }
        /***
         已保存高度
         ***/
    }else{
        /***
         新随机高度
         ***/
        for (int i = 0; i<[numberArray count]; i++) {
            NSInteger randomCount = arc4random()%100+1;
            CGFloat newHeight = (randomCount/300.0f)*originalHeight;
            [saveArray addObject:@{numberArray[i]:@(newHeight)}];
            switch (i+1) {
                case 1:
                    self.heightCons_1.constant = newHeight;
                    break;
                case 2:
                    self.heightCons_2.constant = newHeight;
                    break;
                case 3:
                    self.heightCons_3.constant = newHeight;
                    break;
                case 4:
                    self.heightCons_4.constant = newHeight;
                    break;
                case 5:
                    self.heightCons_5.constant = newHeight;
                    break;
                case 6:
                    self.heightCons_6.constant = newHeight;
                    break;
                case 7:
                    self.heightCons_7.constant = newHeight;
                    break;
                case 8:
                    self.heightCons_8.constant = newHeight;
                    break;
                case 9:
                    self.heightCons_9.constant = newHeight;
                    break;
                case 10:
                    self.heightCons_10.constant = newHeight;
                    break;
                case 11:
                    self.heightCons_11.constant = newHeight;
                    break;
                default:
                    break;
            }
            
            //比较大小
            [self comparingActionWith:@{numberArray[i]:@(newHeight)}];
        }
        
        /***
         新随机高度
         ***/
        [[NSUserDefaults standardUserDefaults] setObject:saveArray forKey:keyStringForToday];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    
    [self loadNumberBox];
}

-(void)comparingActionWith:(NSDictionary*)checkDic{
    
    
    [comparingArray addObject:checkDic];
    
    if ([comparingArray count]>0) {
        [self comparing];
    }
}

-(void)comparing{
    
//    NSLog(@"%@",comparingArray);
    
    NSInteger minIndex = 0;
    CGFloat minValue = 0;
    
    
    for (int i=0; i<[comparingArray count]; i++) {
        NSDictionary* firstDic = [comparingArray objectAtIndex:i];
        CGFloat firstValue = [[firstDic allValues][0] floatValue];
        minValue = firstValue;
        minIndex = i;

        
        NSDictionary* tempDic = [[NSDictionary alloc] initWithDictionary:firstDic];
        NSDictionary* minDic = [[NSDictionary alloc] init];
        for (int j=i+1; j<[comparingArray count]; j++) {
            NSDictionary* secondDic = [comparingArray objectAtIndex:j];
            CGFloat secondValue = [[secondDic allValues][0] floatValue];
            
            if (secondValue>minValue) {
                minValue = secondValue;
                minIndex = j;
                minDic = secondDic;
            }
        }
        if (minIndex!=i) {
            [comparingArray replaceObjectAtIndex:i withObject:minDic];
            [comparingArray replaceObjectAtIndex:minIndex withObject:tempDic];
        }
    }
    
//    NSLog(@"%@",comparingArray);
    
}



-(void)loadNumberBox{
    
//    NSLog(@"%@",comparingArray);
    
    NSMutableArray* showDicArray = [NSMutableArray arrayWithCapacity:0];
    for (int i=0;i<5; i++) {
        NSDictionary* compDic = comparingArray[i];
        [showDicArray addObject:[compDic allKeys][0]];
    }
    
    NSLog(@"%@",showDicArray);
    [selectedNumberArray addObjectsFromArray:[showDicArray sortedArrayUsingSelector:@selector(compare:)]];
    
    CGFloat ballWidth = 40;
    CGFloat spacing = 14;
    CGFloat spacingX = 0;
    for (int i=0; i<[selectedNumberArray count]; i++) {
        UIView* numberView = [[UIView alloc] initWithFrame:CGRectMake(i*(ballWidth+spacing), 0, ballWidth, ballWidth)];
        numberView.backgroundColor = kRGBColor(16,147,255);
        UILabel* numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ballWidth, ballWidth)];
        numberLabel.font = [UIFont boldSystemFontOfSize:15];
        numberLabel.textColor = [UIColor whiteColor];
        numberLabel.textAlignment = NSTextAlignmentCenter;
        numberLabel.text = [selectedNumberArray objectAtIndex:i];
        [numberView addSubview:numberLabel];
        numberView.layer.cornerRadius = ballWidth*0.5;
        [self.numberBoxView addSubview:numberView];
        spacingX+=(ballWidth+spacing);
    }
}

- (IBAction)nextAction:(id)sender {
    NSLog(@"%@",[selectedNumberArray componentsJoinedByString:@","]);
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
